package com.seari.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.seari.main.StartUpClass;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.seari.bean.DataGridBean;
import com.seari.bean.QueryProBean;
import com.seari.bean.SelectObj;
import com.seari.pojo.LagAndRiskPojo;
import com.seari.service.ProSearchService;
import com.seari.utils.ObjectUtil;

/**
 * @author TOUC 下拉框选项及项目搜索功能
 *
 */
@Controller
@RequestMapping("/search")
public class ProSeachController {

	@Autowired
	private ProSearchService proSearchService;

	@RequestMapping("proSearch")
	@ResponseBody
	public DataGridBean<SelectObj> proSearch(String proText) {
		DataGridBean<SelectObj> grid = new DataGridBean<SelectObj>();
		String proSearch = StartUpClass.proSearch;
		if(proSearch.equals("pms")){
			if (StringUtils.isNotBlank(proText)) {
				List<SelectObj> projects = proSearchService.proSearch(proText);
				grid.setCode("succee");
				grid.setRows(projects);
				grid.setTotal(projects.size());
			} else {
				grid.setCode("err");
			}
		}else if (proSearch.equals("sap")){
			if (StringUtils.isNotBlank(proText)) {
				List<SelectObj> projects = proSearchService.proSearchSAP(proText);
				grid.setCode("succee");
				grid.setRows(projects);
				grid.setTotal(projects.size());
			} else {
				grid.setCode("err");
			}
		}
		return grid;
	}

	/**
	 * @param proText
	 * @return 公司下拉框
	 */
	@RequestMapping("major")
	@ResponseBody
	public DataGridBean<SelectObj> getProMajor() {
		DataGridBean<SelectObj> grid = new DataGridBean<SelectObj>();

		String major = StartUpClass.major;
		if(major.equals("pms")){
			List<SelectObj> majors = proSearchService.getProMajor();
			grid.setCode("succee");
			grid.setRows(majors);
			grid.setTotal(majors.size());
		}else if (major.equals("sap")){
			List<SelectObj> majors = proSearchService.getProMajorSAP();
			grid.setCode("succee");
			grid.setRows(majors);
			grid.setTotal(majors.size());
		}
		return grid;
	}

	/**
	 * @param proText
	 * @return 类型下拉框
	 */
	@RequestMapping("nature")
	@ResponseBody
	public DataGridBean<SelectObj> getProNature() {
		DataGridBean<SelectObj> grid = new DataGridBean<SelectObj>();

		String nature = StartUpClass.major;
		if(nature.equals("pms")){
			List<SelectObj> natures = proSearchService.getProNature();
			grid.setCode("succee");
			grid.setRows(natures);
			grid.setTotal(natures.size());
		}else if (nature.equals("sap")){
			List<SelectObj> natures = proSearchService.getProNatureSAP();
			grid.setCode("succee");
			grid.setRows(natures);
			grid.setTotal(natures.size());
		}
		return grid;
	}

	/**
	 * @param proText
	 * @return 维保进度总览
	 */
	@RequestMapping("overview")
	@ResponseBody
	public DataGridBean<Object> getProOverview(
			@RequestBody(required = false) QueryProBean queryProBean) {
		DataGridBean<Object> grid = new DataGridBean<Object>();

		String overview = StartUpClass.overview;
		if(overview.equals("pms")){
	
			// 新开项目数量 滚动项目数量 项目数
			List<Map<String, String>> proMap = proSearchService.getProCount(queryProBean);
			// 根据公司ID、类型ID查询项目号 根据项目号查询项目下所有合同
			// 合同号
			List<String>conids = proSearchService.queryConIdsList(queryProBean);
			
			if(conids == null || conids.size() == 0) {
				grid.setCode("err");
				grid.setMsg("未查询到相关项目合同");
				return grid;
				
			}
			
			queryProBean.setConIdList(conids);
			
			// 资金数
			List<Map<String, String>> numofMoney = proSearchService.queryProConMoney(queryProBean);
			// 投资数
			List<Map<String, String>> investment = proSearchService.queryProInvestment(queryProBean);

			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put("proMap", proMap);
			dataMap.put("numofMoney", numofMoney);
			dataMap.put("investment", investment);

			grid.setData(dataMap);
			grid.setCode("succee");

		}else if (overview.equals("sap")){
			if(queryProBean == null){
				grid.setCode("err");
				grid.setMsg("参数错误");
				return grid;
			}
			// 新开项目数量 滚动项目数量 项目数
			List<Map<String, Object>> proMap = proSearchService.getProCountSAP(queryProBean);

			//查询所有符合条件的订单号
			List<String> conids = proSearchService.queryConIdsListSAP(queryProBean);
			if(conids == null || conids.size() == 0) {
				grid.setCode("err");
				grid.setMsg("未查询到相关项目合同");
				return grid;
			}
			queryProBean.setConIdList(conids);
			// 资金数
			List<Map<String, Object>> numofMoney = proSearchService.queryProConMoneySAP(queryProBean);
			// 投资数
			List<Map<String, Object>> investment = proSearchService.queryProInvestmentSAP(queryProBean);
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put("proMap", proMap);
			dataMap.put("numofMoney", numofMoney);
			dataMap.put("investment", investment);
			grid.setData(dataMap);
			grid.setCode("succee");
		}

		return grid;

	}

	/**
	 * @param proText
	 * @return 维保进度总览差异分析
	 */
	@RequestMapping("prolag")
	@ResponseBody
	public DataGridBean<Object> getProlag(@RequestBody(required = false) QueryProBean queryProBean) {
		DataGridBean<Object> grid = new DataGridBean<Object>();

		String prolag1 = StartUpClass.prolag;
		if(prolag1.equals("pms")){
			// 判断对象中的属性是否为null
			List<String> proIdList = null;
			String natureId = null;
			String companyId = null;

			try{
				natureId = queryProBean.getNatureId();
				companyId = queryProBean.getCompanyId();
			}catch(Exception e){

			}
			// 根据公司ID、类型ID查询项目号 根据项目号查询项目下所有合同
			// 合同号集合 公司 类型 可以为null
			proIdList = proSearchService.getproIdsByCompanyIdAndNatureId(companyId,natureId);
			// 查询差异分析数据
			Map<String, LagAndRiskPojo> prolag = proSearchService.getProlagAnalyse(queryProBean,proIdList);

			grid.setData(prolag);
			grid.setCode("succee");
		}else if (prolag1.equals("sap")){
			// 判断对象中的属性是否为null
			List<String> proIdList = null;
			String natureId = null;
			String companyId = null;
			String year = null;
			String month = null;

			try{
				natureId = queryProBean.getNatureId();
				companyId = queryProBean.getCompanyId();
				year = queryProBean.getYear();
				month = queryProBean.getMonth();
			}catch(Exception e){
//				grid.setCode("err");
//				grid.setMsg("参数错误");
//				return grid;
			}
			//查询所有符合条件的订单号
			List<String> proIds = proSearchService.queryProIdsListSAP(queryProBean);
			if(proIds == null || proIds.size() == 0) {
				grid.setCode("err");
				grid.setMsg("未查询到相关项目合同");
				return grid;
			}
			queryProBean.setProIdList(proIds);
			// 根据公司ID、类型ID查询项目号 根据项目号查询项目下所有合同
			// 合同号集合 公司 类型 可以为null
			Map<String, LagAndRiskPojo> prolag = proSearchService.getproIdsByCompanyIdAndNatureIdSAP(queryProBean);

			grid.setData(prolag);
			grid.setCode("succee");
		}

		return grid;

	}


	/**
	 * @return 维保进度总览查询项目列表
	 */
	@RequestMapping("prolist")
	@ResponseBody
	public DataGridBean<Object> getProlist(@RequestBody(required = false) QueryProBean queryProBean) {
		DataGridBean<Object> grid = new DataGridBean<Object>();

		String prolist = StartUpClass.prolist;
		if(prolist.equals("pms")){
			// 判断对象中的属性是否为null
			List<String> proIdList = queryProBean.getProIdList();
			grid.setData(proSearchService.queryProName(proIdList));
			//grid.setData(prolag);
			grid.setCode("succee");
		}else if (prolist.equals("sap")){

		}

		return grid;

	}
	
	
	
	
	
	
	
	
	
	
	
	

}
